import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:riverpod_t/config/response_model.dart';
import 'package:riverpod_t/page/car/data/model/info_model.dart';
import 'package:riverpod_t/page/car/data/repository/car_repository.dart';
import 'package:riverpod_t/page/car/state/car_detail_state.dart';
part 'car_detail_river.g.dart';

@riverpod
class CarDetailRiver extends _$CarDetailRiver {
  @override
  Future<CarDetailState> build(int entityId) async {
    return CarDetailState(model: await _getDetailInfoData(entityId));
  }

  Future<InfoListModel> _getDetailInfoData(int entityId) async {
    ResponseModel model = await CarRepository.getDetailInfoData<InfoListModel>(entityId);
    return model.data;
  }

  void addLike() async {
    state = const AsyncValue.loading();
    try {
      Future.delayed(Duration(seconds: 1), () {});

      await Future.delayed(Duration(seconds: 1)); // 模拟网络请求延迟
      final currentModel = state.value?.model;
      if (currentModel != null) {
        final newModel = currentModel.copyWith(
          isLike: !currentModel.isLike, // 切换点赞状态
        );
        state = AsyncValue.data(CarDetailState(model: newModel));
      }
    } catch (err, stack) {
      state = AsyncValue.error(err, stack);
    }
  }
}
